﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Experimental8.Common.Data.EDM;
using Experimental8.Infrastructure.DAL.Interfaces;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;

namespace Experimental8.DAL.Services
{
    [Export(typeof(IDACommonDictionariesService))]
    [PartCreationPolicy(System.ComponentModel.Composition.CreationPolicy.Shared)]
    public class DACommonDictionariesService : IDACommonDictionariesService
    {
        private Experimental8ModelContainer _edm = Experimental8ModelContainer.Instance;

        public DACommonDictionariesService()
        {
        }

        //-----------------------------------------------------------------------------------
        #region "Contractors logic"
        public ObservableCollection<contractor> GetContractors()
        {
            IQueryable<contractor> contractorQuery = null;
            try
            {
                contractorQuery = from ctr in _edm.contractorSet select ctr;
                return new ObservableCollection<contractor>(contractorQuery.ToList<contractor>());
            }
            catch (Exception ex)
            {
                Exception exceptionToThrow;
                if (ExceptionPolicy.HandleException(ex, "EntityExceptions Policy", out exceptionToThrow))
                {
                    if (exceptionToThrow == null)
                        throw;
                    else
                        throw exceptionToThrow;
                }

            }
            return null;

        }

        public void AddContractor(contractor contractor)
        {
            /* contractor.contractor_ID = Guid.NewGuid();
             edm.contractor.Add(contractor);*/
        }

        public void RemoveContractor(contractor contractor)
        {
            // edm.contractor.Remove(contractor);
        }

        #endregion
        //-----------------------------------------------------------------------------------
        #region "ContractorGroups logic"
        public ObservableCollection<contractor_group> GetContractorGroups()
        {
            IQueryable<contractor_group> contractorQuery = null;
            try
            {
                contractorQuery = from ctr in this._edm.contractor_groupSet select ctr;
                return new ObservableCollection<contractor_group>(contractorQuery.ToList<contractor_group>());
            }
            catch (Exception ex)
            {
                Exception exceptionToThrow;
                if (ExceptionPolicy.HandleException(ex, "EntityExceptions Policy", out exceptionToThrow))
                {
                    if (exceptionToThrow == null)
                        throw;
                    else
                        throw exceptionToThrow;
                }

            }
            return null;

        }

        public void AddContractorGroup(contractor_group contractorgroup)
        {
            /* contractor.contractor_ID = Guid.NewGuid();
             edm.contractor.Add(contractor);*/
        }

        public void RemoveContractorGroup(contractor_group contractorgroup)
        {
            // edm.contractor.Remove(contractor);
        }

        #endregion
        //-----------------------------------------------------------------------------------


        public void Update()
        {
            try
            {
                int i = this._edm.SaveChanges();
            }
            catch (Exception ex)
            {
                Exception exceptionToThrow;
                if (ExceptionPolicy.HandleException(ex, "EntityExceptions Policy", out exceptionToThrow))
                {
                    if (exceptionToThrow == null)
                        throw;
                    else
                        throw exceptionToThrow;
                }

            }
        }
    }
}
